<?php

namespace App\Admin\Controllers;

use App\EloquentORM\Article;
use App\EloquentORM\ArticleLabel;
use App\EloquentORM\Category;
use App\Repositories\CategoryRepositories;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Str;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article);
	
	    $states = [
		    'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
		    'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
	    ];
	    
        $grid->column('id', __('ID'));
        $grid->column('category_id', __('分类'))->select(Category::selectOptions());
        $grid->column('label', __('标签'))->label('success');
        $grid->column('author', __('作者'))->editable();
        $grid->column('title', __('标题'))->editable();
        $grid->column('top', __('置顶'))->switch($states);
        $grid->column('order', __('排序'))->editable();
        $grid->column('image', __('图片'))->image('',50,50);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('category_id', __('分类'))->as(function ($category)
        {
        	return Category::query()->where(['id'=>$category])->value('name');
        })->label();
        $show->field('label', __('标签'))->badge();
        $show->field('author', __('作者'));
        $show->field('title', __('标题'));
        $show->field('order', __('排序'));
        $show->field('image', __('图片'))->image();
        $show->field('content', __('内容'));
//        $show->field('unique', __('随机数'));
	    $show->field('top', __('置顶'))->using(['否','是'])->label();
	
	    $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article);

        $form->select('category_id', __('分类'))->options(Category::selectOptions());
        $form->tags('label', __('标签'))->options(ArticleLabel::query()->pluck('name','id'));
        $form->text('author', __('作者'))->default(auth('admin')->user()->username);
        $form->text('title', __('标题'));
        $form->switch('top', __('置顶'));
        $form->number('order', __('排序'))->default(1);
        $form->image('image', __('图片'));
        $form->textarea('content', __('内容'));
        $form->text('unique', __('随机数'))->default(Str::uuid()->toString());

        return $form;
    }
}
